package com.sdb.drug.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sdb.common.annotation.Log;
import com.sdb.common.core.controller.BaseController;
import com.sdb.common.core.domain.AjaxResult;
import com.sdb.common.enums.BusinessType;
import com.sdb.drug.domain.TdDrug;
import com.sdb.drug.service.ITdDrugService;
import com.sdb.common.utils.poi.ExcelUtil;
import com.sdb.common.core.page.TableDataInfo;

/**
 * 药品库存Controller
 * 
 * @author yyh
 * @date 2025-02-05
 */
@RestController
@RequestMapping("/drug/stock")
public class TdDrugController extends BaseController
{
    @Autowired
    private ITdDrugService tdDrugService;

    /**
     * 查询药品库存列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping("/list")
    public TableDataInfo list(TdDrug tdDrug)
    {
        startPage();
        List<TdDrug> list = tdDrugService.selectTdDrugList(tdDrug);
        return getDataTable(list);
    }
    /**
     * 根据关键字查询药品列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping("/listByKey")
    public TableDataInfo listByKey(String keyword)
    {
        List<TdDrug> list = tdDrugService.selectListByKey(keyword);
        return getDataTable(list);
    }

    /**
     * 查询库存预警列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping("/listWarn")
    public TableDataInfo listWarn(TdDrug tdDrug)
    {
        startPage();
        List<TdDrug> list = tdDrugService.selectListWarn(tdDrug);
        return getDataTable(list);
    }

    /**
     * 导出药品库存列表
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "药品库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdDrug tdDrug)
    {
        List<TdDrug> list = tdDrugService.selectTdDrugList(tdDrug);
        ExcelUtil<TdDrug> util = new ExcelUtil<TdDrug>(TdDrug.class);
        util.exportExcel(response, list, "药品库存数据");
    }

    /**
     * 获取药品库存详细信息
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @GetMapping(value = "/{code}")
    public AjaxResult getInfo(@PathVariable("code") Long code)
    {
        return success(tdDrugService.selectTdDrugByCode(code));
    }

    /**
     * 新增药品库存
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "药品库存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdDrug tdDrug)
    {
        return toAjax(tdDrugService.insertTdDrug(tdDrug));
    }

    /**
     * 修改药品库存
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "药品库存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdDrug tdDrug)
    {
        return toAjax(tdDrugService.updateTdDrug(tdDrug));
    }

    /**
     * 删除药品库存
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "药品库存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{codes}")
    public AjaxResult remove(@PathVariable Long[] codes)
    {
        for (Long code : codes) {
            TdDrug tdDrug = tdDrugService.selectTdDrugByCode(code);
            if (tdDrug != null && tdDrug.getCount() > 0) {
                // 如果库存不为0，返回错误提示
                return AjaxResult.error("药品库存不为0，不可删除");
            }
        }
        // 如果所有药品的库存都为0，调用删除方法
        return toAjax(tdDrugService.deleteTdDrugByCodes(codes));
    }


    /**
     * 药品入库
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "药品入库", businessType = BusinessType.INSERT)
    @PostMapping("/in")
    public AjaxResult in(@RequestBody TdDrug tdDrug)
    {
        return toAjax(tdDrugService.inStock(tdDrug));
    }

    /**
     * 药品出库
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "药品出库", businessType = BusinessType.INSERT)
    @PostMapping("/out")
    public AjaxResult out(@RequestBody TdDrug tdDrug)
    {
        return toAjax(tdDrugService.outStock(tdDrug));
    }


    /**
     * 发药更新库存
     */
    @PreAuthorize("@ss.hasPermi('index')")
    @Log(title = "发药更新库存", businessType = BusinessType.INSERT)
    @PostMapping("/dispenseMedicines")
    public AjaxResult updateStock(@RequestBody List<TdDrug> tdDrugs)
    {
        int rows = tdDrugService.dispenseMedicines(tdDrugs);
        if (rows > 0) {
            return AjaxResult.success("发药成功，库存已更新");
        } else {
            return AjaxResult.error("发药失败，请检查数据");
        }
    }

}
